﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using Dcms.Model;
using Dcms.BLL;
using Dcms.Common;

namespace WebApp.AppCode
{
    /// <summary>
    /// 后台权限处理
    /// </summary>
    public class CompetenceLib : System.Web.UI.Page
    {
        int intDesignRoleID = Convert.ToInt32(ConfigurationManager.AppSettings["DesignRoleID"]);
        private static readonly string WEB_PATH = ConfigurationManager.AppSettings["WebPath"];
        /// <summary>
        /// 检测是否登录
        /// </summary>
        public void IsLoginMain()
        {
            SessionModle SM = getSessionModle();
            if (SM.IsLogin == false)
            {
                System.Web.HttpContext.Current.Response.Redirect(WEB_PATH + "/AdminLogin.aspx", false);
            }
        }
        /// <summary>
        /// 检测用户是否登录并检测是否为用户登录
        /// </summary>
        public void IsLogin()
        {
            if (Session["NewUser"] != null)
            {
                if ((bool)Session["NewUser"])
                {
                    Session.Add("Message", "请设置您的用户信息或您的密码为初始密码，请更改！");
                    Session.Add("Onclick", "javascript:parent.MM_OpenTabLayers('修改密码','UserManage/EditUsers.aspx','EditPass');");
                    System.Web.HttpContext.Current.Response.Redirect(WEB_PATH + "/Message.aspx", false);
                }
            }
            SessionModle SM = getSessionModle();
            if (SM.IsLogin == false)
            {
                //测试阶段
                string IsTest = ConfigurationManager.AppSettings["IsTest"].ToUpper();
                if (IsTest == "TRUE")
                {//永不过期
                    setSessionModle(true, true, 1, "WebDesigner", 0);
                    Session.Add("DCMSLANGUAGE", "CN");
                }
                else
                {
                    Session.Add("Message", "非法登陆者，请先登陆后操作！");
                    Session.Add("Onclick", "javascript:window.open('AdminLogin.aspx','_top');");
                    System.Web.HttpContext.Current.Response.Redirect(WEB_PATH + "/Message.aspx", false);
                }
                //测试阶段
            }
        }

        /// <summary>
        /// 取到会员等级
        /// </summary>
        /// <returns></returns>
        public int GetUserLevel()
        {
            SessionModle SM = getSessionModle();
            //if (SM.IsAdministrator)
            //{
            //    return -1;
            //}
            //else
            //{
            if (SM.RoleID == 0)
            {
                return -1;
            }
            else
            {
                Dcms.BLL.Dcms_RoleBLL RoleBLL = new Dcms_RoleBLL();

                return RoleBLL.GetLevel(SM.RoleID);
            }
            //}
        }
        /// <summary>
        /// 是否有读取权限
        /// </summary>
        /// <param name="MoldeKey"></param>
        /// <returns></returns>
        public bool IsRead(String Flag)
        {
            SessionModle SM = getSessionModle();
            if (SM.IsAdministrator)
            {
                return true;
            }
            else
            {
                if (SM.RoleID == 0)
                {
                    return false;
                }
                else
                {
                    //取权限ID
                    Dcms_PermissionBLL PermBll = new Dcms_PermissionBLL();
                    int PermissionId = PermBll.GetPermissionIdByFlag(Flag);
                    //取是否有读取权限
                    Dcms_PerRoleModel prM = new Dcms_PerRoleModel();
                    Dcms_PerRoleBLL prBll = new Dcms_PerRoleBLL();
                    prM = prBll.GetModelByPIdAndRId(PermissionId, SM.RoleID);
                    if (prM.PerRole_IsRead == 1)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
        }
        /// <summary>
        /// 是否有读取权限
        /// </summary>
        /// <param name="MoldeKey"></param>
        /// <returns></returns>
        public bool IsRead(String Flag, int RoleID)
        {
            //取权限ID
            Dcms_PermissionBLL PermBll = new Dcms_PermissionBLL();
            int PermissionId = PermBll.GetPermissionIdByFlag(Flag);
            //取是否有读取权限
            Dcms_PerRoleModel prM = new Dcms_PerRoleModel();
            Dcms_PerRoleBLL prBll = new Dcms_PerRoleBLL();
            prM = prBll.GetModelByPIdAndRId(PermissionId, RoleID);
            if (prM != null)
            {
                if (prM.PerRole_IsRead == 1)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 是否有删除的权限
        /// </summary>
        /// <param name="MoldeKey"></param>
        /// <returns></returns>
        public bool IsDelete(String Flag)
        {
            SessionModle SM = getSessionModle();
            if (SM.IsAdministrator)
            {
                return true;
            }
            else
            {
                if (SM.RoleID == 0)
                {
                    return false;
                }
                else
                {
                    //取权限ID
                    Dcms_PermissionBLL PermBll = new Dcms_PermissionBLL();
                    int PermissionId = PermBll.GetPermissionIdByFlag(Flag);
                    //取是否有删除权限
                    Dcms_PerRoleModel prM = new Dcms_PerRoleModel();
                    Dcms_PerRoleBLL prBll = new Dcms_PerRoleBLL();
                    prM = prBll.GetModelByPIdAndRId(PermissionId, SM.RoleID);
                    if (prM.PerRole_IsDelete == 1)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
        }
        /// <summary>
        /// 是否有删除的权限
        /// </summary>
        /// <param name="MoldeKey"></param>
        /// <returns></returns>
        public bool IsDelete(String Flag, int RoleID)
        {
            //取权限ID
            Dcms_PermissionBLL PermBll = new Dcms_PermissionBLL();
            int PermissionId = PermBll.GetPermissionIdByFlag(Flag);
            //取是否有删除权限
            Dcms_PerRoleModel prM = new Dcms_PerRoleModel();
            Dcms_PerRoleBLL prBll = new Dcms_PerRoleBLL();
            prM = prBll.GetModelByPIdAndRId(PermissionId, RoleID);
            if (prM != null)
            {
                if (prM.PerRole_IsDelete == 1)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 是否有修改的权限
        /// </summary>
        /// <param name="MoldeKey"></param>
        /// <returns></returns>
        public bool IsEdit(String Flag)
        {
            SessionModle SM = getSessionModle();
            if (SM.IsAdministrator)
            {
                return true;
            }
            else
            {
                if (SM.RoleID == 0)
                {
                    return false;
                }
                else
                {
                    //取权限ID
                    Dcms_PermissionBLL PermBll = new Dcms_PermissionBLL();
                    int PermissionId = PermBll.GetPermissionIdByFlag(Flag);
                    //取是否有编辑权限
                    Dcms_PerRoleModel prM = new Dcms_PerRoleModel();
                    Dcms_PerRoleBLL prBll = new Dcms_PerRoleBLL();
                    prM = prBll.GetModelByPIdAndRId(PermissionId, SM.RoleID);
                    if (prM.PerRole_IsEdit == 1)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
        }
        /// <summary>
        /// 是否有修改的权限
        /// </summary>
        /// <param name="MoldeKey"></param>
        /// <returns></returns>
        public bool IsEdit(String Flag, int RoleID)
        {
            //取权限ID
            Dcms_PermissionBLL PermBll = new Dcms_PermissionBLL();
            int PermissionId = PermBll.GetPermissionIdByFlag(Flag);
            //取是否有编辑权限
            Dcms_PerRoleModel prM = new Dcms_PerRoleModel();
            Dcms_PerRoleBLL prBll = new Dcms_PerRoleBLL();
            prM = prBll.GetModelByPIdAndRId(PermissionId, RoleID);
            if (prM != null)
            {
                if (prM.PerRole_IsEdit == 1)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 是否有增加的权限
        /// </summary>
        /// <param name="MoldeKey"></param>
        /// <returns></returns>
        public bool IsAdd(String Flag)
        {
            SessionModle SM = getSessionModle();
            if (SM.IsAdministrator)
            {
                return true;
            }
            else
            {
                if (SM.RoleID == 0)
                {
                    return false;
                }
                else
                {
                    //取权限ID
                    Dcms_PermissionBLL PermBll = new Dcms_PermissionBLL();
                    int PermissionId = PermBll.GetPermissionIdByFlag(Flag);
                    //取是否有增加新记录权限
                    Dcms_PerRoleModel prM = new Dcms_PerRoleModel();
                    Dcms_PerRoleBLL prBll = new Dcms_PerRoleBLL();
                    prM = prBll.GetModelByPIdAndRId(PermissionId, SM.RoleID);
                    if (prM.PerRole_IsAdd == 1)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
        }
        /// <summary>
        /// 是否有增加的权限
        /// </summary>
        /// <param name="MoldeKey"></param>
        /// <returns></returns>
        public bool IsAdd(String Flag, int RoleID)
        {
            //取权限ID
            Dcms_PermissionBLL PermBll = new Dcms_PermissionBLL();
            int PermissionId = PermBll.GetPermissionIdByFlag(Flag);
            //取是否有增加新记录权限
            Dcms_PerRoleModel prM = new Dcms_PerRoleModel();
            Dcms_PerRoleBLL prBll = new Dcms_PerRoleBLL();
            prM = prBll.GetModelByPIdAndRId(PermissionId, RoleID);
            if (prM != null)
            {
                if (prM.PerRole_IsAdd == 1)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }

        //是否有读取权限
        public bool IsRead(int CateID)
        {
            SessionModle SM = getSessionModle();
            if (SM.IsAdministrator || SM.RoleID == intDesignRoleID)
            {
                return true;
            }
            else
            {
                if (SM.RoleID == 0)
                {
                    return false;
                }
                else
                {
                    //取是否有删除权限
                    Dcms_PerRoleModel prM = new Dcms_PerRoleModel();
                    Dcms_PerRoleBLL prBll = new Dcms_PerRoleBLL();
                    prM = prBll.GetModelByPIdAndRId(CateID, SM.RoleID);
                    if (prM == null)
                    {
                        //判断其上一级是否有权限
                        Dcms_CateBLL cBll = new Dcms_CateBLL();
                        Dcms_CateModel cModel = cBll.GetModelByID(CateID);
                        while (cModel.Cate_ParentID != 0)
                        {
                            cModel = cBll.GetModelByID(cModel.Cate_ParentID);
                            prM = prBll.GetModelByPIdAndRId(cModel.Cate_ID, SM.RoleID);
                            if (prM != null)
                            {
                                break;
                            }
                        }
                    }
                    if (prM != null)
                    {
                        if (prM.PerRole_IsRead == 1)
                        {
                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    }
                    else
                    {
                        return false;
                    }
                }
            }
        }
        public bool IsRead(int CateID, int RoleID)
        {
            //取是否有读取权限
            Dcms_PerRoleModel prM = new Dcms_PerRoleModel();
            Dcms_PerRoleBLL prBll = new Dcms_PerRoleBLL();
            prM = prBll.GetModelByPIdAndRId(CateID, RoleID);
            if (prM == null)
            {
                //判断其上一级是否有权限
                Dcms_CateBLL cBll = new Dcms_CateBLL();
                Dcms_CateModel cModel = cBll.GetModelByID(CateID);
                while (cModel.Cate_ParentID != 0)
                {
                    cModel = cBll.GetModelByID(cModel.Cate_ParentID);
                    prM = prBll.GetModelByPIdAndRId(cModel.Cate_ID, RoleID);
                    if (prM != null)
                    {
                        break;
                    }
                }
            }
            if (prM != null)
            {
                if (prM.PerRole_IsRead == 1)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }

        //是否有删除权限
        public bool IsDelete(int CateID)
        {
            SessionModle SM = getSessionModle();
            if (SM.IsAdministrator || SM.RoleID == intDesignRoleID)
            {
                return true;
            }
            else
            {
                if (SM.RoleID == 0)
                {
                    return false;
                }
                else
                {
                    //取是否有删除权限
                    Dcms_PerRoleModel prM = new Dcms_PerRoleModel();
                    Dcms_PerRoleBLL prBll = new Dcms_PerRoleBLL();
                    prM = prBll.GetModelByPIdAndRId(CateID, SM.RoleID);
                    if (prM == null)
                    {
                        //判断其上一级是否有权限
                        Dcms_CateBLL cBll = new Dcms_CateBLL();
                        Dcms_CateModel cModel = cBll.GetModelByID(CateID);
                        while (cModel.Cate_ParentID != 0)
                        {
                            cModel = cBll.GetModelByID(cModel.Cate_ParentID);
                            prM = prBll.GetModelByPIdAndRId(cModel.Cate_ID, SM.RoleID);
                            if (prM != null)
                            {
                                break;
                            }
                        }
                    }
                    if (prM != null)
                    {
                        if (prM.PerRole_IsDelete == 1)
                        {
                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    }
                    else
                    {
                        return false;
                    }
                }
            }
        }
        public bool IsDelete(int CateID, int RoleID)
        {
            //取是否有删除权限
            Dcms_PerRoleModel prM = new Dcms_PerRoleModel();
            Dcms_PerRoleBLL prBll = new Dcms_PerRoleBLL();
            prM = prBll.GetModelByPIdAndRId(CateID, RoleID);
            if (prM == null)
            {
                //判断其上一级是否有权限
                Dcms_CateBLL cBll = new Dcms_CateBLL();
                Dcms_CateModel cModel = cBll.GetModelByID(CateID);
                while (cModel.Cate_ParentID != 0)
                {
                    cModel = cBll.GetModelByID(cModel.Cate_ParentID);
                    prM = prBll.GetModelByPIdAndRId(cModel.Cate_ID, RoleID);
                    if (prM != null)
                    {
                        break;
                    }
                }
            }
            if (prM != null)
            {
                if (prM.PerRole_IsDelete == 1)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }

        //是否编辑
        public bool IsEdit(int CateID)
        {
            SessionModle SM = getSessionModle();
            if (SM.IsAdministrator || SM.RoleID == intDesignRoleID)
            {
                return true;
            }
            else
            {
                if (SM.RoleID == 0)
                {
                    return false;
                }
                else
                {
                    //取是否有编辑权限
                    Dcms_PerRoleModel prM = new Dcms_PerRoleModel();
                    Dcms_PerRoleBLL prBll = new Dcms_PerRoleBLL();
                    prM = prBll.GetModelByPIdAndRId(CateID, SM.RoleID);
                    if (prM == null)
                    {
                        //判断其上一级是否有权限
                        Dcms_CateBLL cBll = new Dcms_CateBLL();
                        Dcms_CateModel cModel = cBll.GetModelByID(CateID);
                        while (cModel.Cate_ParentID != 0)
                        {
                            cModel = cBll.GetModelByID(cModel.Cate_ParentID);
                            prM = prBll.GetModelByPIdAndRId(cModel.Cate_ID, SM.RoleID);
                            if (prM != null)
                            {
                                break;
                            }
                        }
                    }
                    if (prM != null)
                    {
                        if (prM.PerRole_IsEdit == 1)
                        {
                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    }
                    else
                    {
                        return false;
                    }
                }
            }
        }
        public bool IsEdit(int CateID, int RoleID)
        {
            //取是否有编辑权限
            Dcms_PerRoleModel prM = new Dcms_PerRoleModel();
            Dcms_PerRoleBLL prBll = new Dcms_PerRoleBLL();
            prM = prBll.GetModelByPIdAndRId(CateID, RoleID);
            if (prM == null)
            {
                //判断其上一级是否有权限
                Dcms_CateBLL cBll = new Dcms_CateBLL();
                Dcms_CateModel cModel = cBll.GetModelByID(CateID);
                while (cModel.Cate_ParentID != 0)
                {
                    cModel = cBll.GetModelByID(cModel.Cate_ParentID);
                    prM = prBll.GetModelByPIdAndRId(cModel.Cate_ID, RoleID);
                    if (prM != null)
                    {
                        break;
                    }
                }
            }
            if (prM != null)
            {
                if (prM.PerRole_IsEdit == 1)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }

        //是否添加
        public bool IsAdd(int CateID)
        {
            SessionModle SM = getSessionModle();
            if (SM.IsAdministrator || SM.RoleID == intDesignRoleID)
            {
                return true;
            }
            else
            {
                if (SM.RoleID == 0)
                {
                    return false;
                }
                else
                {
                    //取是否有增加新记录权限
                    Dcms_PerRoleModel prM = new Dcms_PerRoleModel();
                    Dcms_PerRoleBLL prBll = new Dcms_PerRoleBLL();
                    prM = prBll.GetModelByPIdAndRId(CateID, SM.RoleID);
                    if (prM == null)
                    {
                        //判断其上一级是否有权限
                        Dcms_CateBLL cBll = new Dcms_CateBLL();
                        Dcms_CateModel cModel = cBll.GetModelByID(CateID);
                        while (cModel.Cate_ParentID != 0)
                        {
                            cModel = cBll.GetModelByID(cModel.Cate_ParentID);
                            prM = prBll.GetModelByPIdAndRId(cModel.Cate_ID, SM.RoleID);
                            if (prM != null)
                            {
                                break;
                            }
                        }
                    }
                    if (prM != null)
                    {
                        if (prM.PerRole_IsAdd == 1)
                        {
                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    }
                    else
                    {
                        return false;
                    }
                }
            }
        }
        public bool IsAdd(int CateID, int RoleID)
        {
            //取是否有增加新记录权限
            Dcms_PerRoleModel prM = new Dcms_PerRoleModel();
            Dcms_PerRoleBLL prBll = new Dcms_PerRoleBLL();
            prM = prBll.GetModelByPIdAndRId(CateID, RoleID);
            if (prM == null)
            {
                //判断其上一级是否有权限
                Dcms_CateBLL cBll = new Dcms_CateBLL();
                Dcms_CateModel cModel = cBll.GetModelByID(CateID);
                while (cModel.Cate_ParentID != 0)
                {
                    cModel = cBll.GetModelByID(cModel.Cate_ParentID);
                    prM = prBll.GetModelByPIdAndRId(cModel.Cate_ID, RoleID);
                    if (prM != null)
                    {
                        break;
                    }
                }
            }
            if (prM != null)
            {
                if (prM.PerRole_IsAdd == 1)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 登出后台
        /// </summary>
        /// <returns></returns>
        public String OutLogin()
        {

            string IsUCenter = ConfigurationManager.AppSettings["IsUCenter"].ToUpper();
            if (IsUCenter == "TRUE")
            {
                UserService.SetAuthOut("MyUc");
            }
            if (Session["DCMSLOGINADMIN"] != null)
            {
                Session["DCMSLOGINADMIN"] = null;
                // Session.Abandon();
                return "您已正常退出DesignCMS系统，多谢您的使用！";
            }
            else
            {
                return "";
            }
        }
        /// <summary>
        /// 取用户登录信息
        /// </summary>
        /// <returns></returns>
        public SessionModle getSessionModle()
        {
            if (Session["DCMSLOGINADMIN"] != null)
            {
                return (SessionModle)Session["DCMSLOGINADMIN"];
            }
            else
            {
                ////当Session过期时,查找UC的Cookie
                //string IsUCenter = ConfigurationManager.AppSettings["IsUCenter"].ToUpper();
                //if (IsUCenter == "TRUE")
                //{
                //    try
                //    {
                //        string UserName = UserService.GetCurrentPersonName();
                //        if (UserName != "")
                //        {
                //            string strBackKey = ConfigurationManager.AppSettings["BackKey"];
                //            string strMsg = "";
                //            if (UCenterOperate.CheckLoginApp(UserName, strBackKey, out strMsg))
                //            {
                //                Dcms_AdminBLL AdminBll = new Dcms_AdminBLL();
                //                Dcms_AdminModel admin = AdminBll.GetModelByName(UserName);
                //                if (admin != null)
                //                {
                //                    ArrayList CoderAl = AdminBll.GetCoders();
                //                    CompetenceLib CL = new CompetenceLib();
                //                    CL.setSessionModle(true, CoderAl.Contains(admin.Admin_Name), admin.Admin_Id, admin.Admin_Name, admin.Admin_RoleId);

                //                    return getSessionModle();
                //                }
                //            }
                //        }
                //    }
                //    catch (Exception ex)
                //    { }
                //}
                SessionModle SM = new SessionModle();
                SM.IsLogin = false;
                return SM;
            }
        }
        /// <summary>
        /// 记忆用户登录信息
        /// </summary>
        /// <param name="IsLogin"></param>
        /// <param name="IsAdmin"></param>
        /// <param name="LoginID"></param>
        /// <param name="LoginName"></param>
        /// <param name="RoleID"></param>
        public void setSessionModle(bool IsLogin, bool IsAdmin, int LoginID, string LoginName, int RoleID)
        {
            SessionModle SM = new SessionModle();
            SM.IsLogin = true;
            SM.IsAdministrator = IsAdmin;
            SM.LoginID = LoginID;
            SM.LoginName = LoginName;
            SM.RoleID = RoleID;
            Session.Add("DCMSLOGINADMIN", SM);
        }
    }
    public class SessionModle
    {
        /// <summary>
        /// 是否登录
        /// </summary>
        private bool _ISLogin;
        public bool IsLogin
        {
            set { _ISLogin = value; }
            get { return _ISLogin; }
        }
        /// <summary>
        /// 登录名
        /// </summary>
        private String _LoginName;
        public String LoginName
        {
            set { _LoginName = value; }
            get { return _LoginName; }
        }
        /// <summary>
        /// 登录名
        /// </summary>
        private int _LoginID;
        public int LoginID
        {
            set { _LoginID = value; }
            get { return _LoginID; }
        }
        /// <summary>
        /// 是否是最高级会员
        /// </summary>
        private bool _IsAdministrator;
        public bool IsAdministrator
        {
            set { _IsAdministrator = value; }
            get { return _IsAdministrator; }
        }
        /// <summary>
        /// 角色ID
        /// </summary>
        private int _RoleID;
        public int RoleID
        {
            set { _RoleID = value; }
            get { return _RoleID; }
        }
    }
}
